package com.github.tonydeng.fmj.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.system.oshi.OshiUtil;
import com.github.tonydeng.fmj.common.Constants;
import com.github.tonydeng.fmj.model.ResponseMap;
import com.github.tonydeng.fmj.pool.RedisOpsUtil;
import com.github.tonydeng.fmj.pool.ThreadPoolOps;
import com.github.tonydeng.fmj.utils.ConverterUtils;
import com.github.tonydeng.fmj.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import oshi.hardware.NetworkIF;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static cn.hutool.core.net.NetUtil.getLocalMacAddress;

@EnableAutoConfiguration
@RestController
@RequestMapping(value = "/plot")
public class PlotController {

    @Autowired
    private ThreadPoolOps poolOps;

    @Autowired
    public RedisOpsUtil redisOpsUtil;

    public static ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

    @RequestMapping("")
    @ResponseBody
    public Map<String, Object> index(HttpServletRequest request, HttpServletResponse response, @RequestParam String key, @RequestParam String ip, String debug) {
        ResponseMap result = new ResponseMap(200, "系统已准备好!");

        String origin = request.getHeader("Origin");
        if (StringUtils.isEmpty(origin)) {
            origin = "*";
        }
        response.setHeader("Access-Control-Allow-Origin", origin);
        response.setHeader("Access-Control-Allow-Methods", "*");
        //请求包含的字段内容，如有多个可用哪个逗号分隔如下
        response.setHeader("Access-Control-Allow-Headers", "content-type,x-requested-with,Authorization, x-ui-request," + "a188");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setCharacterEncoding("UTF-8");
        if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(200);
            return result.toMap();
        }

        if (StrUtil.isNotEmpty(key)) {
            Constants.JWT_TOKEN = ip;
            redisOpsUtil.setValue("plotId_" + Constants.JWT_TOKEN, ip);
            redisOpsUtil.setValue("plotKey_" + Constants.JWT_TOKEN, key);

            if (StrUtil.isNotEmpty(debug)) {
                redisOpsUtil.setValue("plotId_debug_" + Constants.JWT_TOKEN, "1", 180L);
            }

            result.setMessage("系统已准备好! plotId:" + Constants.JWT_TOKEN);
        } else {
            result.setMessage("系统已准备好! plotId:" + Constants.JWT_TOKEN + ", 转移数量:" + Constants.MOVE_COUNT);
        }

        return result.toMap();
    }


    @RequestMapping("check")
    @ResponseBody
    public Map<String, Object> check(HttpServletRequest request, HttpServletResponse response, @RequestParam String ip, Integer del) {
        ResponseMap result = new ResponseMap(200, "系统已准备好!");

        String origin = request.getHeader("Origin");
        if (StringUtils.isEmpty(origin)) {
            origin = "*";
        }
        response.setHeader("Access-Control-Allow-Origin", origin);
        response.setHeader("Access-Control-Allow-Methods", "*");
        //请求包含的字段内容，如有多个可用哪个逗号分隔如下
        response.setHeader("Access-Control-Allow-Headers", "content-type,x-requested-with,Authorization, x-ui-request," + "a188");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setCharacterEncoding("UTF-8");
        if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(200);
            return result.toMap();
        }

        //將已有的文件名稱緩存到系統中
        redisOpsUtil.deleteKeys("moveOk_" + ip);
        redisOpsUtil.deleteKeys("moveing_" + ip);

        Map<String, String> map = new HashMap<>();
        List<Map<String, String>> targetDirList = new ArrayList<>();
        map = new HashMap<>();
        map.put("id", "1");
        map.put("value", "/home/lvm/ar1/");
        targetDirList.add(map);
        map = new HashMap<>();
        map.put("id", "2");
        map.put("value", "/home/lvm/ar2/");
        targetDirList.add(map);
        map = new HashMap<>();
        map.put("id", "3");
        map.put("value", "/home/lvm/ar3/");
        targetDirList.add(map);
        map = new HashMap<>();
        map.put("id", "4");
        map.put("value", "/home/lvm/ar4/");
        targetDirList.add(map);
        map = new HashMap<>();
        map.put("id", "5");
        map.put("value", "/home/lvm/ar5/");
        targetDirList.add(map);

        Integer count = 0;
        Integer count2 = 0;
        Integer find1 = 0;
        Integer find2 = 0;
        Integer find3 = 0;
        Integer find4 = 0;
        Integer find5 = 0;
        for (Map<String, String> data : targetDirList
        ) {
            String targetId = data.get("id");
            String targetDir = data.get("value");

            //判断目录是否存在
            if (!FileUtil.exist(targetDir)) {
//                        String log = StrUtil.format("{}, {} 不存在", DateUtil.now(), targetDir);
                //System.out.println(log);
                continue;
            }

            List<String> list = FileUtil.listFileNames(targetDir);
            for (String fileName : list
            ) {
                try {
                    if (!fileName.contains(".ar") || fileName.contains(".tmp")) {
                        continue;
                    }

                    //重複文件刪除
                    String moveOk = redisOpsUtil.getValue("moveOk_" + ip + "_" + Md5Util.md5(fileName));
                    if (StrUtil.isNotEmpty(moveOk)) {
                        System.out.println(targetDir + fileName + ",文件重複刪除");
                        if (!StringUtils.isEmpty(del)) {
                            File tagertFile = FileUtil.file(targetDir + fileName);
                            FileUtil.del(tagertFile);
                        }
                        count2++;
                        continue;
                    }

                    count++;
                    if (targetId.equals("1")) {
                        find1++;
                    } else if (targetId.equals("2")) {
                        find2++;
                    } else if (targetId.equals("3")) {
                        find3++;
                    } else if (targetId.equals("4")) {
                        find4++;
                    } else if (targetId.equals("5")) {
                        find5++;
                    }

                    redisOpsUtil.setValue("moveOk_" + ip + "_" + Md5Util.md5(fileName), targetDir);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }

        result.setMessage(StrUtil.format("检查完成! 总文件:{},重复文件:{}.ar1:{},ar2:{},ar3:{},ar4:{},ar5:{}", count, count2, find1, find2, find3, find4, find5));
        return result.toMap();
    }

    @RequestMapping("check2")
    @ResponseBody
    public Map<String, Object> check2(HttpServletRequest request, HttpServletResponse response, @RequestParam String ip, Integer del, Long key) {
        ResponseMap result = new ResponseMap(200, "系统已准备好!");

        String origin = request.getHeader("Origin");
        if (StringUtils.isEmpty(origin)) {
            origin = "*";
        }
        response.setHeader("Access-Control-Allow-Origin", origin);
        response.setHeader("Access-Control-Allow-Methods", "*");
        //请求包含的字段内容，如有多个可用哪个逗号分隔如下
        response.setHeader("Access-Control-Allow-Headers", "content-type,x-requested-with,Authorization, x-ui-request," + "a188");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setCharacterEncoding("UTF-8");
        if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(200);
            return result.toMap();
        }

        //將已有的文件名稱緩存到系統中
        redisOpsUtil.deleteKeys("moveOk_" + ip);
        redisOpsUtil.deleteKeys("moveing_" + ip);

        Map<String, String> map = new HashMap<>();
        List<Map<String, String>> targetDirList = new ArrayList<>();
        map = new HashMap<>();
        map.put("id", "0");
        map.put("value", "/home/ar0/");
        targetDirList.add(map);
        map = new HashMap<>();
        map.put("id", "1");
        map.put("value", "/home/ar1/");
        targetDirList.add(map);
        map = new HashMap<>();
        map.put("id", "2");
        map.put("value", "/home/ar2/");
        targetDirList.add(map);
        map = new HashMap<>();
        map.put("id", "3");
        map.put("value", "/home/ar3/");
        targetDirList.add(map);
        map = new HashMap<>();
        map.put("id", "4");
        map.put("value", "/home/ar4/");
        targetDirList.add(map);
        map = new HashMap<>();
        map.put("id", "5");
        map.put("value", "/home/ar5/");
        targetDirList.add(map);
        map = new HashMap<>();
        map.put("id", "6");
        map.put("value", "/home/ar6/");
        targetDirList.add(map);

        Integer count = 0;
        Integer count2 = 0;
        Integer count3 = 0;
        Integer find1 = 0;
        Integer find2 = 0;
        Integer find3 = 0;
        Integer find4 = 0;
        Integer find5 = 0;
        Integer find6 = 0;
        Integer find0 = 0;
        for (Map<String, String> data : targetDirList
        ) {
            String targetId = data.get("id");
            String targetDir = data.get("value");

            //判断目录是否存在
            if (!FileUtil.exist(targetDir)) {
//                        String log = StrUtil.format("{}, {} 不存在", DateUtil.now(), targetDir);
                //System.out.println(log);
                continue;
            }

            List<String> list = FileUtil.listFileNames(targetDir);
            for (String fileName : list
            ) {
                try {
                    if (!fileName.contains(".ar") || fileName.contains(".tmp")) {
                        continue;
                    }

                    //重複文件刪除
                    String moveOk = redisOpsUtil.getValue("moveOk_" + ip + "_" + Md5Util.md5(fileName));
                    if (StrUtil.isNotEmpty(moveOk)) {
                        System.out.println(targetDir + fileName + ",文件重複刪除");
                        if (!StringUtils.isEmpty(del)) {
                            File tagertFile = FileUtil.file(targetDir + fileName);
                            FileUtil.del(tagertFile);
                        }
                        count2++;
                        continue;
                    }

                    //v1v2v3文件删除
                    if (fileName.contains("-v1") || fileName.contains("-v2") || fileName.contains("-v3")){
                        System.out.println(targetDir + fileName + ",v3文件刪除");
                        if (!StringUtils.isEmpty(del)) {
                            File tagertFile = FileUtil.file(targetDir + fileName);
                            FileUtil.del(tagertFile);
                        }
                        count3++;
                        continue;
                    }

                    count++;
                    if (targetId.equals("1")) {
                        find1++;
                    } else if (targetId.equals("2")) {
                        find2++;
                    } else if (targetId.equals("3")) {
                        find3++;
                    } else if (targetId.equals("4")) {
                        find4++;
                    } else if (targetId.equals("5")) {
                        find5++;
                    } else if (targetId.equals("0")) {
                        find0++;
                    } else if (targetId.equals("6")) {
                        find6++;
                    }

                    redisOpsUtil.setValue("moveOk_" + ip + "_" + Md5Util.md5(fileName), targetDir);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }

        result.setMessage(StrUtil.format("检查完成! 总文件:{},重复文件:{}.ar0:{},ar1:{},ar2:{},ar3:{},ar4:{},ar5:{},ar6:{},v2删除文件:{}", count, count2, find0, find1, find2, find3, find4, find5, find6, count3));
        return result.toMap();
    }

    @RequestMapping("cleanLimit")
    @ResponseBody
    public Map<String, Object> cleanLimit(HttpServletRequest request, HttpServletResponse response) {
        ResponseMap result = new ResponseMap(200, "解除固态盘进程限制!");

        String origin = request.getHeader("Origin");
        if (StringUtils.isEmpty(origin)) {
            origin = "*";
        }
        response.setHeader("Access-Control-Allow-Origin", origin);
        response.setHeader("Access-Control-Allow-Methods", "*");
        //请求包含的字段内容，如有多个可用哪个逗号分隔如下
        response.setHeader("Access-Control-Allow-Headers", "content-type,x-requested-with,Authorization, x-ui-request," + "a188");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setCharacterEncoding("UTF-8");
        if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(200);
            return result.toMap();
        }

        String plostId = redisOpsUtil.getValue("plotKey_" + Constants.JWT_TOKEN);
        redisOpsUtil.deleteKeys("src_moveing_" + plostId);
        return result.toMap();
    }

    @RequestMapping("stopMove")
    @ResponseBody
    public Map<String, Object> stopMove(HttpServletRequest request, HttpServletResponse response) {
        ResponseMap result = new ResponseMap(200, "移动停止!");

        String origin = request.getHeader("Origin");
        if (StringUtils.isEmpty(origin)) {
            origin = "*";
        }
        response.setHeader("Access-Control-Allow-Origin", origin);
        response.setHeader("Access-Control-Allow-Methods", "*");
        //请求包含的字段内容，如有多个可用哪个逗号分隔如下
        response.setHeader("Access-Control-Allow-Headers", "content-type,x-requested-with,Authorization, x-ui-request," + "a188");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setCharacterEncoding("UTF-8");
        if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(200);
            return result.toMap();
        }

        redisOpsUtil.setValue("stopMove", "1");
        return result.toMap();
    }

    @RequestMapping("startMove")
    @ResponseBody
    public Map<String, Object> restartMove(HttpServletRequest request, HttpServletResponse response) {
        ResponseMap result = new ResponseMap(200, "移动开始!");

        String origin = request.getHeader("Origin");
        if (StringUtils.isEmpty(origin)) {
            origin = "*";
        }
        response.setHeader("Access-Control-Allow-Origin", origin);
        response.setHeader("Access-Control-Allow-Methods", "*");
        //请求包含的字段内容，如有多个可用哪个逗号分隔如下
        response.setHeader("Access-Control-Allow-Headers", "content-type,x-requested-with,Authorization, x-ui-request," + "a188");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setCharacterEncoding("UTF-8");
        if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(200);
            return result.toMap();
        }

        redisOpsUtil.delete("stopMove");
        return result.toMap();
    }

    @RequestMapping("/cleanDb")
    @ResponseBody
    public Map<String, Object> cleanDb(HttpServletRequest request, HttpServletResponse response) {
        ResponseMap result = new ResponseMap(200, "redis已清理!");

        String origin = request.getHeader("Origin");
        if (StringUtils.isEmpty(origin)) {
            origin = "*";
        }
        response.setHeader("Access-Control-Allow-Origin", origin);
        response.setHeader("Access-Control-Allow-Methods", "*");
        //请求包含的字段内容，如有多个可用哪个逗号分隔如下
        response.setHeader("Access-Control-Allow-Headers", "content-type,x-requested-with,Authorization, x-ui-request," + "a188");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setCharacterEncoding("UTF-8");
        if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(200);
            return result.toMap();
        }

        redisOpsUtil.clean();

        return result.toMap();
    }

    @RequestMapping("/sign")
    @ResponseBody
    public Map<String, Object> sign(HttpServletRequest request, HttpServletResponse response) {
        ResponseMap result = new ResponseMap(200, "操作成功!");

        String origin = request.getHeader("Origin");
        if (StringUtils.isEmpty(origin)) {
            origin = "*";
        }
        response.setHeader("Access-Control-Allow-Origin", origin);
        response.setHeader("Access-Control-Allow-Methods", "*");
        //请求包含的字段内容，如有多个可用哪个逗号分隔如下
        response.setHeader("Access-Control-Allow-Headers", "content-type,x-requested-with,Authorization, x-ui-request," + "a188");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setCharacterEncoding("UTF-8");
        if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(200);
            return result.toMap();
        }

        String mac = getLocalMacAddress();
        redisOpsUtil.setValue("sign_check", Md5Util.md5(mac + "tc1688"));
        return result.toMap();
    }

    @RequestMapping("/clean")
    @ResponseBody
    public Map<String, Object> clean(HttpServletRequest request, HttpServletResponse response) {
        ResponseMap result = new ResponseMap(200, "清理成功!");

        String origin = request.getHeader("Origin");
        if (StringUtils.isEmpty(origin)) {
            origin = "*";
        }
        response.setHeader("Access-Control-Allow-Origin", origin);
        response.setHeader("Access-Control-Allow-Methods", "*");
        //请求包含的字段内容，如有多个可用哪个逗号分隔如下
        response.setHeader("Access-Control-Allow-Headers", "content-type,x-requested-with,Authorization, x-ui-request," + "a188");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setCharacterEncoding("UTF-8");
        if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(200);
            return result.toMap();
        }

        cachedThreadPool.execute(() -> {
            Set<String> workingKeys = redisOpsUtil.getAll("working_");
            for (String workingKey : workingKeys
            ) {
                String tempDir = redisOpsUtil.getValue("tempDir_" + workingKey.replace("working_", ""));
                String temp2Dir = redisOpsUtil.getValue("temp2Dir_" + workingKey.replace("working_", ""));

                if (StrUtil.isNotEmpty(tempDir)) {
                    FileUtil.del(tempDir);
                }

                if (StrUtil.isNotEmpty(temp2Dir)) {
                    FileUtil.del(temp2Dir);
                }

                redisOpsUtil.delete(workingKey);
            }
        });

        return result.toMap();
    }

    /**
     * @api {get} /plot/setTaskConfig 设置调度参数
     * @apiGroup p盘控制
     * @apiName 设置调度参数
     * @apiParam {String} sleepTime 秒单位 并发任务的时候，每个任务之间的启动时间间隔 10s http://192.168.199.164:8088/plot/setTaskConfig?sleepTime=420&keepCount=24&diskCount=4&totalCount=60
     * @apiParam {String} totalCount 总任务数，受限于机械硬盘容量  14T 140个任务
     * @apiParam {String} max_concurrent 总并发数，受限于总内存数, 256G 77个保险点76个任务
     * @apiParam {String} max_for_phase_1 第一阶段最大进程进程数,受限于单块硬盘写入数， 128桶 2线程 3400G内存 最多4-6个
     * @apiParam {String} max_concurrent_with_start_early 工作流包含提前的任务最大并发数，12+（4-6）+ (4-6) = 20 - 24
     * @apiParam {String} concurrency_start_early_phase_delay 秒单位 提前开启任务的间隔时间 480s
     * @apiParam {String} diskCount p盘机的硬盘总数量
     * @apiSuccessExample {json} Success-Response: HTTP/1.1 202 ACCEPTED
     * {message: "ACCEPTED", httpCode: 202 }
     * @apiVersion 0.1.0
     */
    @RequestMapping("/setTaskConfig")
    @ResponseBody
    public Map<String, Object> setTaskConfig(@RequestParam String sleepTime, @RequestParam String max_for_phase_1, @RequestParam String diskCount, @RequestParam String totalCount,
                                             @RequestParam String max_concurrent, @RequestParam String max_concurrent_with_start_early, @RequestParam String concurrency_start_early_phase_delay,
                                             HttpServletRequest request, HttpServletResponse response) {

        ResponseMap result = new ResponseMap(200, "设置调度参数完成!");

        String origin = request.getHeader("Origin");
        if (StringUtils.isEmpty(origin)) {
            origin = "*";
        }
        response.setHeader("Access-Control-Allow-Origin", origin);
        response.setHeader("Access-Control-Allow-Methods", "*");
        //请求包含的字段内容，如有多个可用哪个逗号分隔如下
        response.setHeader("Access-Control-Allow-Headers", "content-type,x-requested-with,Authorization, x-ui-request," + "a188");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setCharacterEncoding("UTF-8");
        if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(200);
            return result.toMap();
        }

        redisOpsUtil.setValue("sleepTime", sleepTime);
        redisOpsUtil.setValue("max_for_phase_1", max_for_phase_1);
        redisOpsUtil.setValue("max_concurrent", max_concurrent);
        redisOpsUtil.setValue("max_concurrent_with_start_early", max_concurrent_with_start_early);
        redisOpsUtil.setValue("concurrency_start_early_phase_delay", concurrency_start_early_phase_delay);
        redisOpsUtil.setValue("diskCount", diskCount);
        redisOpsUtil.setValue("totalCount", totalCount);

        return result.toMap();
    }

    /**
     * @api {get} /plot/getWorkList 获取正在工作的任务信息
     * @apiGroup p盘控制
     * @apiName 获取正在工作的任务信息
     * @apiSuccessExample {json} Success-Response: HTTP/1.1 202 ACCEPTED
     * {message: "ACCEPTED", httpCode: 202 }
     * @apiVersion 0.1.0
     */
    @RequestMapping("/getWorkList")
    @ResponseBody
    public Map<String, Object> getWorkList(HttpServletRequest request, HttpServletResponse response) {

        ResponseMap result = new ResponseMap(200, "获取正在工作的任务信息完成!");

        String origin = request.getHeader("Origin");
        if (StringUtils.isEmpty(origin)) {
            origin = "*";
        }
        response.setHeader("Access-Control-Allow-Origin", origin);
        response.setHeader("Access-Control-Allow-Methods", "*");
        //请求包含的字段内容，如有多个可用哪个逗号分隔如下
        response.setHeader("Access-Control-Allow-Headers", "content-type,x-requested-with,Authorization, x-ui-request," + "a188");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setCharacterEncoding("UTF-8");
        if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(200);
            return result.toMap();
        }

        //获取任务id
        List<Map<String, String>> workIdList = new ArrayList<>();
        Map<String, Map<String, String>> workList = poolOps.getWorkList();
        if (workList != null && !workList.isEmpty()) {
            workList.forEach((id, values) -> {
                workIdList.add(values);
            });
        }

        List<Map<String, String>> workInfoList = new ArrayList<>();
        //遍历任务获取整体工作状态
        for (Map<String, String> data : workIdList
        ) {
            String workId = data.get("workId");
            String dCount = data.get("dCount");
            String startTime = data.get("startTime");

            Map<String, String> map = new HashMap<>();
            //判断已全部完成的进程
            String plotEnd = redisOpsUtil.getValue("plot_end_" + dCount + "_" + workId);
            if (StrUtil.isNotEmpty(plotEnd)) {
                continue;
            }

            map.put("workId", workId);
            //判断已开始工作的总进程
            String plotStart = redisOpsUtil.getValue("plot_start_" + dCount + "_" + workId);
            if (StrUtil.isNotEmpty(plotStart)) {
                map.put("plotStat", "已启动P盘,开始时间:" + startTime);
            }
            //判断已完成第1阶段的进程
            String phase1 = redisOpsUtil.getValue("plot_phase_1_" + dCount + "_" + workId);
            if (StrUtil.isNotEmpty(phase1)) {
                String phase1Time = redisOpsUtil.getValue("plot_phase_1_time_" + dCount + "_" + workId);
                map.put("plotPhase1", "第1阶段完成,耗时" + phase1Time + "s");
            }
            //判断已完成第2阶段的进程
            String phase2 = redisOpsUtil.getValue("plot_phase_2_" + dCount + "_" + workId);
            if (StrUtil.isNotEmpty(phase2)) {
                String phase2Time = redisOpsUtil.getValue("plot_phase_2_time_" + dCount + "_" + workId);
                map.put("plotPhase2", "第2阶段完成,耗时" + phase2Time + "s");
            }
            //判断已完成第3阶段的进程
            String phase3 = redisOpsUtil.getValue("plot_phase_3_" + dCount + "_" + workId);
            if (StrUtil.isNotEmpty(phase3)) {
                String phase3Time = redisOpsUtil.getValue("plot_phase_3_time_" + dCount + "_" + workId);
                map.put("plotPhase3", "第3阶段完成,耗时" + phase3Time + "s");
            }
            //判断已完成第4阶段的进程
            String phase4 = redisOpsUtil.getValue("plot_phase_4_" + dCount + "_" + workId);
            if (StrUtil.isNotEmpty(phase4)) {
                String phase4Time = redisOpsUtil.getValue("plot_phase_4_time_" + dCount + "_" + workId);
                map.put("plotPhase4", "第4阶段完成,耗时" + phase4Time + "s");
            }
            //总耗时
            String end = redisOpsUtil.getValue("plot_end_" + dCount + "_" + workId);
            if (StrUtil.isNotEmpty(end)) {
                String totalTime = redisOpsUtil.getValue("plot_phase_total_time_" + dCount + "_" + workId);
                map.put("totalTime", "总耗时" + totalTime + "s");
            }

            workInfoList.add(map);
        }

        result.putResult(workInfoList);
        return result.toMap();
    }

    /**
     * @api {get} /plot/getFinishList 获取已完成工作的任务信息
     * @apiGroup p盘控制
     * @apiName 获取已完成工作的任务信息
     * @apiSuccessExample {json} Success-Response: HTTP/1.1 202 ACCEPTED
     * {message: "ACCEPTED", httpCode: 202 }
     * @apiVersion 0.1.0
     */
    @RequestMapping("/getFinishList")
    @ResponseBody
    public Map<String, Object> getFinishList(HttpServletRequest request, HttpServletResponse response) {

        ResponseMap result = new ResponseMap(200, "获取已完成的任务信息完成!");

        String origin = request.getHeader("Origin");
        if (StringUtils.isEmpty(origin)) {
            origin = "*";
        }
        response.setHeader("Access-Control-Allow-Origin", origin);
        response.setHeader("Access-Control-Allow-Methods", "*");
        //请求包含的字段内容，如有多个可用哪个逗号分隔如下
        response.setHeader("Access-Control-Allow-Headers", "content-type,x-requested-with,Authorization, x-ui-request," + "a188");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setCharacterEncoding("UTF-8");
        if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(200);
            return result.toMap();
        }

        List<Map<String, String>> finishIdList = new ArrayList<>();
        Map<String, Map<String, String>> workList = poolOps.getWorkList();
        if (workList != null && !workList.isEmpty()) {
            workList.forEach((id, values) -> {
                finishIdList.add(values);
            });
        }

        List<Map<String, String>> finishInfoList = new ArrayList<>();
        //遍历任务获取整体工作状态
        for (Map<String, String> data : finishIdList
        ) {
            String workId = data.get("workId");
            String startTime = data.get("startTime");
            String dCount = data.get("dCount");

            Map<String, String> map = new HashMap<>();
            map.put("workId", workId);
            //判断已开始工作的总进程
            String plotStart = redisOpsUtil.getValue("plot_start_" + dCount + "_" + workId);
            if (StrUtil.isNotEmpty(plotStart)) {
                map.put("plotStat", "已启动P盘,开始时间:" + startTime);
            }
            //判断已完成第1阶段的进程
            String phase1 = redisOpsUtil.getValue("plot_phase_1_" + dCount + "_" + workId);
            if (StrUtil.isNotEmpty(phase1)) {
                String phase1Time = redisOpsUtil.getValue("plot_phase_1_time_" + dCount + "_" + workId);
                map.put("plotPhase1", "第1阶段完成,耗时" + phase1Time + "s");
            }
            //判断已完成第2阶段的进程
            String phase2 = redisOpsUtil.getValue("plot_phase_2_" + dCount + "_" + workId);
            if (StrUtil.isNotEmpty(phase2)) {
                String phase2Time = redisOpsUtil.getValue("plot_phase_2_time_" + dCount + "_" + workId);
                map.put("plotPhase2", "第2阶段完成,耗时" + phase2Time + "s");
            }
            //判断已完成第3阶段的进程
            String phase3 = redisOpsUtil.getValue("plot_phase_3_" + dCount + "_" + workId);
            if (StrUtil.isNotEmpty(phase3)) {
                String phase3Time = redisOpsUtil.getValue("plot_phase_3_time_" + dCount + "_" + workId);
                map.put("plotPhase3", "第3阶段完成,耗时" + phase3Time + "s");
            }
            //判断已完成第4阶段的进程
            String phase4 = redisOpsUtil.getValue("plot_phase_4_" + dCount + "_" + workId);
            if (StrUtil.isNotEmpty(phase4)) {
                String phase4Time = redisOpsUtil.getValue("plot_phase_4_time_" + dCount + "_" + workId);
                map.put("plotPhase4", "第4阶段完成,耗时" + phase4Time + "s");
            }
            //总耗时
            String end = redisOpsUtil.getValue("plot_end_" + dCount + "_" + workId);
            if (StrUtil.isNotEmpty(end)) {
                String totalTime = redisOpsUtil.getValue("plot_phase_total_time_" + dCount + "_" + workId);
                map.put("totalTime", "总耗时" + totalTime + "s");
            }

            finishInfoList.add(map);
        }

        result.putResult(finishInfoList);
        return result.toMap();
    }

    /**
     * @api {get} /plot/getPlotStatus 获取整体任务状态
     * @apiGroup p盘控制
     * @apiName 获取整体任务状态
     * @apiSuccessExample {json} Success-Response: HTTP/1.1 202 ACCEPTED
     * {message: "ACCEPTED", httpCode: 202 }
     * @apiVersion 0.1.0
     */
    @RequestMapping("/getPlotStatus")
    @ResponseBody
    public Map<String, Object> getPlotStatus(HttpServletRequest request, HttpServletResponse response) {
        ResponseMap result = new ResponseMap(200, "获取整体任务状态完成!");

        String origin = request.getHeader("Origin");
        if (StringUtils.isEmpty(origin)) {
            origin = "*";
        }
        response.setHeader("Access-Control-Allow-Origin", origin);
        response.setHeader("Access-Control-Allow-Methods", "*");
        //请求包含的字段内容，如有多个可用哪个逗号分隔如下
        response.setHeader("Access-Control-Allow-Headers", "content-type,x-requested-with,Authorization, x-ui-request," + "a188");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setCharacterEncoding("UTF-8");
        if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(200);
            return result.toMap();
        }

        //获取任务id
        List<Map<String, String>> workIdList = new ArrayList<>();
        Map<String, Map<String, String>> workList = poolOps.getWorkList();
        if (workList != null && !workList.isEmpty()) {
            workList.forEach((id, values) -> {
                workIdList.add(values);
            });
        }

        Integer plotStartCount = 0;
        Integer plotPhase1Count = 0;
        Integer plotPhase2Count = 0;
        Integer plotPhase3Count = 0;
        Integer plotPhase4Count = 0;
        Integer plotEndCount = 0;
        Integer plotWorkingPhase1Count = 0;
        Integer plotWorkingPhase2Count = 0;
        Integer plotWorkingPhase3Count = 0;
        Integer plotWorkingPhase4Count = 0;
        Integer plotWorkingCount = 0;
        Integer plotErrorCount = 0;

        List<Map<String, Object>> workInfoList = new ArrayList<>();
        //遍历任务获取整体工作状态
        for (Map<String, String> data : workIdList
        ) {
            String workId = data.get("workId");
            String startTime = data.get("startTime");
            String dCount = data.get("dCount");

            //判断当前已经工作的总进程
            String plotStart = redisOpsUtil.getValue("plot_start_" + dCount + "_" + workId);
            if (StrUtil.isNotEmpty(plotStart)) {
                plotStartCount++;
            }
            //判断已完成第1阶段的进程
            String phase1 = redisOpsUtil.getValue("plot_phase_1_" + dCount + "_" + workId);
            if (StrUtil.isNotEmpty(phase1)) {
                plotPhase1Count++;
            }
            //判断已完成第2阶段的进程
            String phase2 = redisOpsUtil.getValue("plot_phase_2_" + dCount + "_" + workId);
            if (StrUtil.isNotEmpty(phase2)) {
                plotPhase2Count++;
            }
            //判断已完成第3阶段的进程
            String phase3 = redisOpsUtil.getValue("plot_phase_3_" + dCount + "_" + workId);
            if (StrUtil.isNotEmpty(phase3)) {
                plotPhase3Count++;
            }
            //判断已完成第4阶段的进程
            String phase4 = redisOpsUtil.getValue("plot_phase_4_" + dCount + "_" + workId);
            if (StrUtil.isNotEmpty(phase4)) {
                plotPhase4Count++;
            }
            //判断已全部完成的进程
            String plotEnd = redisOpsUtil.getValue("plot_end_" + dCount + "_" + workId);
            if (StrUtil.isNotEmpty(plotEnd)) {
                plotEndCount++;
            }
            //判断还在工作的进程
            String plotWorking = redisOpsUtil.getValue("working_" + dCount + "_" + workId);
            if (StrUtil.isNotEmpty(plotWorking)) {
                plotWorkingCount++;
            }
            //判断错误的进程
            String plotError = redisOpsUtil.getValue("plot_error_" + dCount + "_" + workId);
            if (StrUtil.isNotEmpty(plotError)) {
                plotErrorCount++;
            }
        }

        Map<String, Object> map = new HashMap<>();
        //整体任务开始时间
        String firstStartTime = redisOpsUtil.getValue("firstStartTime");
        map.put("firstStartTime", firstStartTime);
        //已开始进程数
        map.put("plotStartCount", plotStartCount.toString());
        //完成第1阶段进程数
        map.put("plotPhase1FinishCount", plotPhase1Count.toString());
        //完成第2阶段进程数
        map.put("plotPhase2FinishCount", plotPhase2Count.toString());
        //完成第3阶段进程数
        map.put("plotPhase3FinishCount", plotPhase3Count.toString());
        //完成第4阶段进程数
        map.put("plotPhase4FinishCount", plotPhase4Count.toString());
        //当前正在工作的进程数
        map.put("plotWorkingCount", plotWorkingCount.toString());
        //p盘完成进程数
        map.put("plotEnd", plotEndCount.toString());
        //出现错误的进程数
        map.put("plotErrorCount", plotErrorCount.toString());

        //正在第1阶段工作的进程数
        plotWorkingPhase1Count = plotStartCount - plotPhase1Count;
        map.put("plotWorkingPhase1Count", plotWorkingPhase1Count.toString());

        //正在第2阶段工作的进程数
        plotWorkingPhase2Count = plotPhase1Count - plotPhase2Count;
        map.put("plotWorkingPhase2Count", plotWorkingPhase2Count.toString());

        //正在第3阶段工作的进程数
        plotWorkingPhase3Count = plotPhase2Count - plotPhase3Count;
        map.put("plotWorkingPhase3Count", plotWorkingPhase3Count.toString());

        //正在第4阶段工作的进程数
        plotWorkingPhase4Count = plotPhase3Count - plotPhase4Count;
        map.put("plotWorkingPhase4Count", plotWorkingPhase4Count.toString());

        //获取每个硬盘工作的进程数
        List<Map<String, String>> diskWorkCountList = new ArrayList<>();
        String diskCount = redisOpsUtil.getValue("diskCount");
        Integer dCount = ConverterUtils.toInteger(diskCount);
        for (Integer currentCount = 1; currentCount <= dCount; currentCount++) {
            Set<String> all = redisOpsUtil.getAll("plot_working_" + currentCount + "_");
            Map<String, String> data = new HashMap<>();

            //硬盘标识
            data.put("diskNum", currentCount.toString());
            //硬盘工作的进程数
            data.put("diskWorkingCount", ConverterUtils.toString(all.size()));

            //使用的硬盘目录
            String usedDisk = redisOpsUtil.getValue("tmpDir" + currentCount);
            File diskPartition = new File(usedDisk);
            long totalCapacity = diskPartition.getTotalSpace();
            long usablePatitionSpace = diskPartition.getUsableSpace();

            //硬盘总容量
            data.put("diskTotalCapacity", FileUtil.readableFileSize(totalCapacity));
            //硬盘可用容量
            data.put("diskUsablePatitionSpace", FileUtil.readableFileSize(usablePatitionSpace));
            diskWorkCountList.add(data);
        }
        map.put("diskWorkCountList", diskWorkCountList);

        //内存使用情况
        long totalMemory = OshiUtil.getMemory().getTotal();
        long availableMemory = OshiUtil.getMemory().getAvailable();
        map.put("totalMemory", FileUtil.readableFileSize(totalMemory));
        map.put("availableMemory", FileUtil.readableFileSize(availableMemory));

        //获取cpu温度, 风扇速度,cpu信息
        map.put("cpuTemperature", OshiUtil.getSensors().getCpuTemperature());
        map.put("fanSpeeds", OshiUtil.getSensors().getFanSpeeds());
        map.put("cpuInfo", OshiUtil.getCpuInfo().toString());

//        List<Map<String, String>> netWorkList = new ArrayList<>();
//        List<NetworkIF> list = OshiUtil.getNetworkIFs();
//        for (NetworkIF netWorkIf:list
//        ) {
//            Map<String, String> data = new HashMap<>();
//
//            //网卡
//            data.put("netWorkName", netWorkIf.getDisplayName());
//            //接收数据量
//            data.put("netWorkRecv", FileUtil.readableFileSize(netWorkIf.getBytesRecv()));
//            //发送数据量
//            data.put("netWorkSend", FileUtil.readableFileSize(netWorkIf.getBytesSent()));
//            //速率
//            data.put("netWorkSpeed", FileUtil.readableFileSize(netWorkIf.getSpeed()));
//
//            netWorkList.add(data);
//        }
//        map.put("netWorkList", netWorkList);

        workInfoList.add(map);
        result.putResult(workInfoList);
        return result.toMap();
    }

    /**
     * @api {get} /plot/setPlotConfig 设置p盘参数
     * @apiGroup p盘控制
     * @apiName 设置p盘参数
     * @apiParam {String} size   k,定义绘图的大小。不同系统上的k大小和创建时间列表请查看：k大小对应的P盘文件规格 32 http://192.168.199.164:8088/plot/setPlotConfig?diskCount=6&state=1&size=32&num=1&bytes=3400&theadNum=32&buckets=128&farmerKey=96a2a6ddbd7a0a5757fe892fc6dcdc60102022d31ff6a2393d01074e3caedb12270dd0139e2f268c8813eed84011c420&poolKey=b87531b5c484d766891c59909da107a7cbd64959d3f0b2510f55c423fee3f9cfea72f2129f38d3c183fa06009d02717e&tmpDir=/home/nvme0n1/ChiaTemps&tmp2Dir=/home/nvme1n1/ChiaPlots&finalDir=/home/ChiaFarms
     * @apiParam {String} num    n,按顺序进行的绘图数量。一旦一个绘图完成，它将被移动到最终位置-d，然后再开始下一个绘图序列 1
     * @apiParam {String} bytes   b,[内存缓冲区大小MiB]。定义内存/RAM使用量。默认值是2048 (2GiB)。更多的内存将略微提高绘图的速度。请记住，这只是分配给绘图算法的内存。运行钱包等将需要你的系统提供额外的内存。 5120
     * @apiParam {String} theadNum   r,[线程数]: 2线程通常是最佳的 2
     * @apiParam {String} buckets    u,[buckets数量]。更多的数据包可以减少对内存的需求，但需要更多的随机磁盘搜索。对于机械磁盘，你需要设置更少的buckets，而对于NVMe固态硬盘，你可以设置更多的buckets。一般来说，你可以设置32、64或128（默认） 128
     * @apiParam {String} farmerKey   f,[farmer 公钥]: 这是你的 "农民公钥". 当你想在其他机器上创建P盘文件时，如果你不想给chia账户完整的访问权限，就可以使用这个密钥 96a2a6ddbd7a0a5757fe892fc6dcdc60102022d31ff6a2393d01074e3caedb12270dd0139e2f268c8813eed84011c420
     * @apiParam {String} poolKey   p,[pool 公钥]。这是你的 "池公钥". 当你想在其他机器上创建P盘文件时，如果你不想给chia账户完整的访问权限时，就可以使用它 96a2a6ddbd7a0a5757fe892fc6dcdc60102022d31ff6a2393d01074e3caedb12270dd0139e2f268c8813eed84011c420
     * @apiParam {String} tmpDir    t,定义P盘时的临时目录。这里是P盘的第一阶段和第二阶段需要使用 E:\ChiaPlots
     * @apiParam {String} tmp2Dir   t2,定义一个次要的临时目录，用于存放P盘临时文件。这是绘图阶段3（压缩）和阶段4（检查）发生的地方 E:\ChiaPlots2
     * @apiParam {String} finalDir  d,定义存储plot文件的最终位置 F:\
     * @apiParam {String} diskCount  第几个硬盘的配置
     * @apiParam {String} state  该配置是否启用 1 启用 0 禁用
     * @apiParam 每块硬盘的执行配置，主流p盘机有4块硬盘，需要执行四次配置
     * @apiSuccessExample {json} Success-Response: HTTP/1.1 202 ACCEPTED
     * {message: "ACCEPTED", httpCode: 202 }
     * @apiVersion 0.1.0
     */
    @RequestMapping("/setPlotConfig")
    @ResponseBody
    public Map<String, Object> setPlotConfig(@RequestParam String size, @RequestParam String num, @RequestParam String bytes, @RequestParam String theadNum,
                                             @RequestParam String buckets, @RequestParam String farmerKey, @RequestParam String poolKey,
                                             @RequestParam String tmpDir, @RequestParam String tmp2Dir, @RequestParam String finalDir, @RequestParam String diskCount,
                                             @RequestParam String state, HttpServletRequest request, HttpServletResponse response) {

        ResponseMap result = new ResponseMap(200, StrUtil.format("设置硬盘{}的p盘参数完成!", diskCount));

        String origin = request.getHeader("Origin");
        if (StringUtils.isEmpty(origin)) {
            origin = "*";
        }
        response.setHeader("Access-Control-Allow-Origin", origin);
        response.setHeader("Access-Control-Allow-Methods", "*");
        //请求包含的字段内容，如有多个可用哪个逗号分隔如下
        response.setHeader("Access-Control-Allow-Headers", "content-type,x-requested-with,Authorization, x-ui-request," + "a188");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setCharacterEncoding("UTF-8");
        if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(200);
            return result.toMap();
        }

        redisOpsUtil.setValue("size" + diskCount, size);
        redisOpsUtil.setValue("num" + diskCount, num);
        redisOpsUtil.setValue("bytes" + diskCount, bytes);
        redisOpsUtil.setValue("theadNum" + diskCount, theadNum);
        redisOpsUtil.setValue("buckets" + diskCount, buckets);
        redisOpsUtil.setValue("farmerKey" + diskCount, farmerKey);
        redisOpsUtil.setValue("poolKey" + diskCount, poolKey);
        redisOpsUtil.setValue("tmpDir" + diskCount, tmpDir);
        redisOpsUtil.setValue("tmp2Dir" + diskCount, tmp2Dir);
        redisOpsUtil.setValue("finalDir" + diskCount, finalDir);
        redisOpsUtil.setValue("state" + diskCount, state);

        return result.toMap();
    }

    /**
     * @api {get} /plot/setPlotConfigAll 设置全部硬盘p盘参数
     * @apiGroup p盘控制
     * @apiName 设置全部硬盘p盘参数
     * @apiParam {String} size   k,定义绘图的大小。不同系统上的k大小和创建时间列表请查看：k大小对应的P盘文件规格 32 http://192.168.199.164:8088/plot/setPlotConfig?diskCount=6&state=1&size=32&num=1&bytes=3400&theadNum=32&buckets=128&farmerKey=96a2a6ddbd7a0a5757fe892fc6dcdc60102022d31ff6a2393d01074e3caedb12270dd0139e2f268c8813eed84011c420&poolKey=b87531b5c484d766891c59909da107a7cbd64959d3f0b2510f55c423fee3f9cfea72f2129f38d3c183fa06009d02717e&tmpDir=/home/nvme0n1/ChiaTemps&tmp2Dir=/home/nvme1n1/ChiaPlots&finalDir=/home/ChiaFarms
     * @apiParam {String} num    n,按顺序进行的绘图数量。一旦一个绘图完成，它将被移动到最终位置-d，然后再开始下一个绘图序列 1
     * @apiParam {String} bytes   b,[内存缓冲区大小MiB]。定义内存/RAM使用量。默认值是2048 (2GiB)。更多的内存将略微提高绘图的速度。请记住，这只是分配给绘图算法的内存。运行钱包等将需要你的系统提供额外的内存。 5120
     * @apiParam {String} theadNum   r,[线程数]: 2线程通常是最佳的 2
     * @apiParam {String} buckets    u,[buckets数量]。更多的数据包可以减少对内存的需求，但需要更多的随机磁盘搜索。对于机械磁盘，你需要设置更少的buckets，而对于NVMe固态硬盘，你可以设置更多的buckets。一般来说，你可以设置32、64或128（默认） 128
     * @apiParam {String} farmerKey   f,[farmer 公钥]: 这是你的 "农民公钥". 当你想在其他机器上创建P盘文件时，如果你不想给chia账户完整的访问权限，就可以使用这个密钥 96a2a6ddbd7a0a5757fe892fc6dcdc60102022d31ff6a2393d01074e3caedb12270dd0139e2f268c8813eed84011c420
     * @apiParam {String} poolKey   p,[pool 公钥]。这是你的 "池公钥". 当你想在其他机器上创建P盘文件时，如果你不想给chia账户完整的访问权限时，就可以使用它 96a2a6ddbd7a0a5757fe892fc6dcdc60102022d31ff6a2393d01074e3caedb12270dd0139e2f268c8813eed84011c420
     * @apiParam {String} tmpDir    t,定义P盘时的临时目录。这里是P盘的第一阶段和第二阶段需要使用 E:\ChiaPlots
     * @apiParam {String} tmp2Dir   t2,定义一个次要的临时目录，用于存放P盘临时文件。这是绘图阶段3（压缩）和阶段4（检查）发生的地方 E:\ChiaPlots2
     * @apiParam {String} finalDir  d,定义存储plot文件的最终位置 F:\
     * @apiParam {String} diskCount  共几块硬盘的配置
     * @apiParam {String} state  该配置是否启用 1 启用 0 禁用
     * @apiSuccessExample {json} Success-Response: HTTP/1.1 202 ACCEPTED
     * {message: "ACCEPTED", httpCode: 202 }
     * @apiVersion 0.1.0
     */
    @RequestMapping("/setPlotConfigAll")
    @ResponseBody
    public Map<String, Object> setPlotConfigAll(@RequestParam String size, @RequestParam String num, @RequestParam String bytes, @RequestParam String theadNum,
                                                @RequestParam String buckets, @RequestParam String farmerKey, @RequestParam String poolKey,
                                                @RequestParam String tmpDir, @RequestParam String tmp2Dir, @RequestParam String finalDir, @RequestParam String diskCount,
                                                @RequestParam String state, HttpServletRequest request, HttpServletResponse response) {

        ResponseMap result = new ResponseMap(200, StrUtil.format("设置硬盘{}的p盘参数完成!", diskCount));

        String origin = request.getHeader("Origin");
        if (StringUtils.isEmpty(origin)) {
            origin = "*";
        }
        response.setHeader("Access-Control-Allow-Origin", origin);
        response.setHeader("Access-Control-Allow-Methods", "*");
        //请求包含的字段内容，如有多个可用哪个逗号分隔如下
        response.setHeader("Access-Control-Allow-Headers", "content-type,x-requested-with,Authorization, x-ui-request," + "a188");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setCharacterEncoding("UTF-8");
        if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(200);
            return result.toMap();
        }

        Integer dCount = ConverterUtils.toInteger(diskCount);
        for (Integer currentCount = 1; currentCount <= dCount; currentCount++) {
            redisOpsUtil.setValue("size" + currentCount, size);
            redisOpsUtil.setValue("num" + currentCount, num);
            redisOpsUtil.setValue("bytes" + currentCount, bytes);
            redisOpsUtil.setValue("theadNum" + currentCount, theadNum);
            redisOpsUtil.setValue("buckets" + currentCount, buckets);
            redisOpsUtil.setValue("farmerKey" + currentCount, farmerKey);
            redisOpsUtil.setValue("poolKey" + currentCount, poolKey);
            redisOpsUtil.setValue("tmpDir" + currentCount, tmpDir);
            redisOpsUtil.setValue("tmp2Dir" + currentCount, tmp2Dir);
            redisOpsUtil.setValue("finalDir" + currentCount, finalDir);
            redisOpsUtil.setValue("state" + currentCount, state);
        }

        return result.toMap();
    }


    @RequestMapping("/startar")
    @ResponseBody
    public Map<String, Object> startar(HttpServletRequest request, HttpServletResponse response) {
        ResponseMap result = new ResponseMap(200, "移动已准备好!");

        String origin = request.getHeader("Origin");
        if (StringUtils.isEmpty(origin)) {
            origin = "*";
        }
        response.setHeader("Access-Control-Allow-Origin", origin);
        response.setHeader("Access-Control-Allow-Methods", "*");
        //请求包含的字段内容，如有多个可用哪个逗号分隔如下
        response.setHeader("Access-Control-Allow-Headers", "content-type,x-requested-with,Authorization, x-ui-request," + "a188");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setCharacterEncoding("UTF-8");
        if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(200);
            return result.toMap();
        }

        List<Map<String, String>> urlList = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        map = new HashMap<>();
        map.put("id", "5");
        map.put("value", "http://10.10.0.5:8081/plot?key=5&ip=check05");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "40");
        map.put("value", "http://10.10.0.40:8081/plot?key=6&ip=check40");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "1");
        map.put("value", "http://10.10.0.1:8081/plot?key=6&ip=check01");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "37");
        map.put("value", "http://10.10.0.37:8081/plot?key=6&ip=check37");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "2");
        map.put("value", "http://10.10.0.2:8081/plot?key=6&ip=check02");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "4");
        map.put("value", "http://10.10.0.4:8081/plot?key=6&ip=check04");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "30");
        map.put("value", "http://10.10.0.30:8081/plot?key=6&ip=check30");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "31");
        map.put("value", "http://10.10.0.31:8081/plot?key=6&ip=check31");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "32");
        map.put("value", "http://10.10.0.32:8081/plot?key=6&ip=check32");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "3");
        map.put("value", "http://10.10.0.3:8081/plot?key=6&ip=check03");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "34");
        map.put("value", "http://10.10.0.34:8081/plot?key=6&ip=check34");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "35");
        map.put("value", "http://10.10.0.35:8081/plot?key=6&ip=check35");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "6");
        map.put("value", "http://10.10.0.6:8081/plot?key=6&ip=check06");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "27");
        map.put("value", "http://10.10.0.27:8081/plot?key=6&ip=check27");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "26");
        map.put("value", "http://10.10.0.26:8081/plot?key=6&ip=check26");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "39");
        map.put("value", "http://10.10.0.39:8081/plot?key=6&ip=check39");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "24");
        map.put("value", "http://10.10.0.24:8081/plot?key=6&ip=check24");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "23");
        map.put("value", "http://10.10.0.23:8081/plot?key=6&ip=check23");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "22");
        map.put("value", "http://10.10.0.22:8081/plot?key=6&ip=check22");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "15");
        map.put("value", "http://10.10.0.15:8081/plot?key=6&ip=check15");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "16");
        map.put("value", "http://10.10.0.16:8081/plot?key=6&ip=check16");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "17");
        map.put("value", "http://10.10.0.17:8081/plot?key=6&ip=check17");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "18");
        map.put("value", "http://10.10.0.18:8081/plot?key=6&ip=check18");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "19");
        map.put("value", "http://10.10.0.19:8081/plot?key=6&ip=check19");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "20");
        map.put("value", "http://10.10.0.20:8081/plot?key=6&ip=check20");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "21");
        map.put("value", "http://10.10.0.21:8081/plot?key=6&ip=check21");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "14");
        map.put("value", "http://10.10.0.14:8081/plot?key=6&ip=check14");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "13");
        map.put("value", "http://10.10.0.13:8081/plot?key=6&ip=check13");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "12");
        map.put("value", "http://10.10.0.12:8081/plot?key=6&ip=check12");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "11");
        map.put("value", "http://10.10.0.11:8081/plot?key=6&ip=check11");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "10");
        map.put("value", "http://10.10.0.10:8081/plot?key=6&ip=check10");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "9");
        map.put("value", "http://10.10.0.9:8081/plot?key=6&ip=check09");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "8");
        map.put("value", "http://10.10.0.8:8081/plot?key=6&ip=check08");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "25");
        map.put("value", "http://10.10.0.25:8081/plot?key=6&ip=check25");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "38");
        map.put("value", "http://10.10.0.38:8081/plot?key=6&ip=check38");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "29");
        map.put("value", "http://10.10.0.29:8081/plot?key=6&ip=check29");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "33");
        map.put("value", "http://10.10.0.33:8081/plot?key=6&ip=check33");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "28");
        map.put("value", "http://10.10.0.28:8081/plot?key=6&ip=check28");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "36");
        map.put("value", "http://10.10.0.36:8081/plot?key=6&ip=check36");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "37");
        map.put("value", "http://10.10.0.7:8081/plot?key=6&ip=check07");
        urlList.add(map);

        for (Map<String, String> data : urlList
        ) {
            try{
                cachedThreadPool.execute(() -> {
                    String result1 = HttpUtil.get(data.get("value"));
                });
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }

        return result.toMap();
    }

    @RequestMapping("/checkar")
    @ResponseBody
    public Map<String, Object> checkar(HttpServletRequest request, HttpServletResponse response) {
        ResponseMap result = new ResponseMap(200, "清理已准备好!");

        String origin = request.getHeader("Origin");
        if (StringUtils.isEmpty(origin)) {
            origin = "*";
        }
        response.setHeader("Access-Control-Allow-Origin", origin);
        response.setHeader("Access-Control-Allow-Methods", "*");
        //请求包含的字段内容，如有多个可用哪个逗号分隔如下
        response.setHeader("Access-Control-Allow-Headers", "content-type,x-requested-with,Authorization, x-ui-request," + "a188");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setCharacterEncoding("UTF-8");
        if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(200);
            return result.toMap();
        }

        List<Map<String, String>> urlList = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        map = new HashMap<>();
        map.put("id", "5");
        map.put("value", "http://10.10.0.5:8081/plot/check2?ip=check05&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "40");
        map.put("value", "http://10.10.0.40:8081/plot/check2?ip=check40&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "1");
        map.put("value", "http://10.10.0.1:8081/plot/check2?ip=check01&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "37");
        map.put("value", "http://10.10.0.37:8081/plot/check2?ip=check37&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "2");
        map.put("value", "http://10.10.0.2:8081/plot/check2?ip=check02&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "4");
        map.put("value", "http://10.10.0.4:8081/plot/check2?ip=check04&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "30");
        map.put("value", "http://10.10.0.30:8081/plot/check2?ip=check30&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "31");
        map.put("value", "http://10.10.0.31:8081/plot/check2?ip=check31&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "32");
        map.put("value", "http://10.10.0.32:8081/plot/check2?ip=check32&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "3");
        map.put("value", "http://10.10.0.3:8081/plot/check2?ip=check03&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "34");
        map.put("value", "http://10.10.0.34:8081/plot/check2?ip=check34&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "35");
        map.put("value", "http://10.10.0.35:8081/plot/check2?ip=check35&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "6");
        map.put("value", "http://10.10.0.6:8081/plot/check2?ip=check06&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "27");
        map.put("value", "http://10.10.0.27:8081/plot/check2?ip=check27&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "26");
        map.put("value", "http://10.10.0.26:8081/plot/check2?ip=check26&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "39");
        map.put("value", "http://10.10.0.39:8081/plot/check2?ip=check39&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "24");
        map.put("value", "http://10.10.0.24:8081/plot/check2?ip=check24&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "23");
        map.put("value", "http://10.10.0.23:8081/plot/check2?ip=check23&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "22");
        map.put("value", "http://10.10.0.22:8081/plot/check2?ip=check22&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "15");
        map.put("value", "http://10.10.0.15:8081/plot/check2?ip=check15&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "16");
        map.put("value", "http://10.10.0.16:8081/plot/check2?ip=check16&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "17");
        map.put("value", "http://10.10.0.17:8081/plot/check2?ip=check17&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "18");
        map.put("value", "http://10.10.0.18:8081/plot/check2?ip=check18&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "19");
        map.put("value", "http://10.10.0.19:8081/plot/check2?ip=check19&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "20");
        map.put("value", "http://10.10.0.20:8081/plot/check2?ip=check20&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "21");
        map.put("value", "http://10.10.0.21:8081/plot/check2?ip=check21&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "14");
        map.put("value", "http://10.10.0.14:8081/plot/check2?ip=check14&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "13");
        map.put("value", "http://10.10.0.13:8081/plot/check2?ip=check13&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "12&del=1");
        map.put("value", "http://10.10.0.12:8081/plot/check2?ip=check12&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "11");
        map.put("value", "http://10.10.0.11:8081/plot/check2?ip=check11&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "10");
        map.put("value", "http://10.10.0.10:8081/plot/check2?ip=check10&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "9&del=1");
        map.put("value", "http://10.10.0.9:8081/plot/check2?ip=check09&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "8");
        map.put("value", "http://10.10.0.8:8081/plot/check2?ip=check08&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "25");
        map.put("value", "http://10.10.0.25:8081/plot/check2?ip=check25&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "38");
        map.put("value", "http://10.10.0.38:8081/plot/check2?ip=check38&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "29");
        map.put("value", "http://10.10.0.29:8081/plot/check2?ip=check29&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "33");
        map.put("value", "http://10.10.0.33:8081/plot/check2?ip=check33&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "28");
        map.put("value", "http://10.10.0.28:8081/plot/check2?ip=check28&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "36");
        map.put("value", "http://10.10.0.36:8081/plot/check2?ip=check36&del=1");
        urlList.add(map);
        map = new HashMap<>();
        map.put("id", "37");
        map.put("value", "http://10.10.0.7:8081/plot/check2?ip=check07&del=1");
        urlList.add(map);

        for (Map<String, String> data : urlList
        ) {
            try{
                cachedThreadPool.execute(() -> {
                    String result1 = HttpUtil.get(data.get("value"));
                });
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }

        return result.toMap();
    }
}
